<?php

namespace app\factory\logic;

use app\common\exception\LogicException;
use app\model\Fence;
use think\facade\Db;

class FenceLogic
{
    protected Fence $model;
    protected int $factory_id;

    public function __construct()
    {
        $this->model = new Fence();
        $this->factory_id = request()->factory_info['factory_id'];
    }

    /**
     * @Description 新增/编辑
     * @param int $Id
     * @param array $param
     * @time 2024/03/15 14:41:18
     * @return true
     * @author: zj
     */
    public function addEdit(int $Id, array $param)
    {
        Db::startTrans();
        try {
            $exist = $this->model->checkOutIdIsExist($this->factory_id, $Id);
            if ($exist) { // 猪栏存在,执行更新
                $rule = [
                    'BarnId' => 'integer|>:0',
                    'Name' => 'max:50',
                    'DeviceId' => 'max:255',
                    'FeedBaseTime' => '>:0',
                    'FeedPreDayId' => 'integer|>=:0',
                    'isLoop' => 'integer|in:0,1',
                ];
                validate($rule)->check($param);

                $updateData = [];
                if ($param['BarnId']) {
                    $updateData['out_barn_id'] = $param['BarnId'];
                }
                if ($param['Name']) {
                    $barnId = $this->model->where('out_id', $Id)
                        ->where('delete_time', $this->model::IS_DELETE_NO)
                        ->value('out_barn_id', 0);
                    $barnId = $param['BarnId'] ?: $barnId;
                    $name = $this->model->checkNameIsExist($barnId, $param['Name']);
                    if ($name && $name != $exist) {
                        throw new LogicException('猪栏已存在');
                    }
                    $updateData['name'] = $param['Name'];
                }
                if ($param['DeviceId']) {
                    $updateData['device_id'] = $param['DeviceId'];
                }
                if ($param['FeedBaseTime']) {
                    $updateData['feed_base_time'] = $param['FeedBaseTime'];
                }
                if ($param['FeedPreDayId']) {
                    $updateData['feed_plan_id'] = $param['FeedPreDayId'];
                }
                if (isset($param['isLoop'])) {
                    $updateData['is_loop'] = $param['isLoop'];
                }
                if (isset($param['Remark'])) {
                    $updateData['remark'] = $param['Remark'];
                }
                $updateData['update_record_time'] = $param['RecordTime'];
                $updateData['update_time'] = request()->time();

                $this->model->where('out_id', $Id)
                    ->where('delete_time', $this->model::IS_DELETE_NO)
                    ->update($updateData);
            } else {// 猪栏不存在，执行新增
                $rule = [
                    'BarnId' => 'require|integer|>:0',
                    'Name' => 'require|max:50',
                    'DeviceId' => 'require|max:255',
                    'FeedBaseTime' => 'require|integer|>:0',
                    'FeedPreDayId' => 'require|integer|>=:0',
                    'isLoop' => 'require|integer|in:0,1',
                ];
                validate($rule)->check($param);

                $this->model->insert([
                    'out_id' => $Id,
                    'factory_id' => $this->factory_id,
                    'out_barn_id' => $param['BarnId'],
                    'name' => $param['Name'],
                    'device_id' => $param['DeviceId'],
                    'feed_base_time' => $param['FeedBaseTime'],
                    'feed_plan_id' => $param['FeedPreDayId'],
                    'is_loop' => $param['isLoop'],
                    'remark' => $param['Remark'],
                    'record_time' => $param['RecordTime'],
                    'update_record_time' => $param['RecordTime'],
                    'create_time' => request()->time(),
                    'update_time' => request()->time()
                ]);
            }

            Db::commit();
        } catch (\Exception $exception) {
            Db::commit();
            throw new LogicException($exception->getMessage());
        }

        return true;
    }
}